﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThanhTu.GNC.Media.KQXS.DataAccess.DO;
using System.Data;
using Oracle.ManagedDataAccess.Client;

namespace ThanhTu.GNC.Media.KQXS.DataAccess.DAL
{
    public class CommentDAL : BaseDAL
    {
        public CommentDAL() : base() { }
        public int SaveComment(CommentDO vCommentDO)
        {
            using (OracleCommand oraComm = new OracleCommand())
            {
                oraComm.CommandType = CommandType.StoredProcedure;
                oraComm.CommandText = "COMMENTS_PKG.SAVECOMMENTS";
                OracleParameter oraParam = null;

                oraParam = new OracleParameter("IN_EMAIL", OracleDbType.Varchar2);
                oraParam.Value = vCommentDO.Email;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("IN_PHONE", OracleDbType.Varchar2);
                oraParam.Value = vCommentDO.Phone;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("IN_CONTENT", OracleDbType.Varchar2);
                oraParam.Value = vCommentDO.Content;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("OUTRETURN", OracleDbType.Int32);
                oraParam.Direction = ParameterDirection.Output;
                oraComm.Parameters.Add(oraParam);

                int result = ExecuteNoneQuery(oraComm);

                if (!Convert.IsDBNull(oraComm.Parameters["OUTRETURN"]))
                { result = Convert.ToInt32(oraComm.Parameters["OUTRETURN"].Value.ToString()); }

                oraComm.Dispose();
                oraParam = null;

                return result;
            }
        }

        public List<CommentDO> SelectAll()
        {
            using (OracleCommand oraComm = new OracleCommand())
            {
                oraComm.CommandType = CommandType.StoredProcedure;
                oraComm.CommandText = "COMMENTS_PKG.GETALLCOMMENTS";
                OracleParameter oraParam = null;

                oraParam = new OracleParameter("OUTRETURN", OracleDbType.RefCursor);
                oraParam.Direction = ParameterDirection.Output;
                oraComm.Parameters.Add(oraParam);

                using (DataSet ds = GetDataSet(oraComm))
                {
                    DataTable dt = null;
                    List<CommentDO> vCommentDOCollection = null;
                    DataRow dr = null;
                    CommentDO vCommentDO = null;

                    if (ds != null)
                    {
                        if (ds.Tables.Count > 0)
                        {
                            dt = ds.Tables[0];
                            vCommentDOCollection = new List<CommentDO>();

                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                dr = dt.Rows[i];
                                vCommentDO = new CommentDO();

                                if (!Convert.IsDBNull(dr["PHONE"]))
                                { vCommentDO.Phone = Convert.ToString(dr["PHONE"]); }

                                if (!Convert.IsDBNull(dr["EMAIL"]))
                                { vCommentDO.Email = Convert.ToString(dr["EMAIL"]); }

                                if (!Convert.IsDBNull(dr["CONTENT"]))
                                { vCommentDO.Content = Convert.ToString(dr["CONTENT"]); }


                                vCommentDOCollection.Add(vCommentDO);
                                dr = null;
                                vCommentDO = null;
                            }

                            dt.Dispose();
                            dt = null;
                        }

                        ds.Dispose();
                    }
                    oraComm.Dispose();
                    oraParam = null;

                    return vCommentDOCollection;
                }
            }
        }

        public DataTable selectAll_Page(int pageIndex, int pageSize, ref int rowCount)
        {
            using (OracleCommand oraComm = new OracleCommand())
            {
                oraComm.CommandType = CommandType.StoredProcedure;
                oraComm.CommandText = "COMMENTS_PKG.GETALLCOMMENTSPAGE";
                OracleParameter oraParam = null;

                oraParam = new OracleParameter("INPAGEINDEX", OracleDbType.Int32);
                oraParam.Value = pageIndex;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("INPAGESIZE", OracleDbType.Int32);
                oraParam.Value = pageSize;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("OUTRETURN", OracleDbType.RefCursor);
                oraParam.Direction = ParameterDirection.Output;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("OUTROWCOUNT", OracleDbType.Int32);
                oraParam.Direction = ParameterDirection.Output;
                oraComm.Parameters.Add(oraParam);

                using (DataSet ds = GetDataSet(oraComm))
                {
                    DataTable dt = null;

                    if (ds != null)
                    {
                        if (ds.Tables.Count > 0)
                        {
                            dt = ds.Tables[0];

                            if (!Convert.IsDBNull(oraComm.Parameters["OUTROWCOUNT"]))
                            { rowCount = Convert.ToInt32(oraComm.Parameters["OUTROWCOUNT"].Value.ToString()); }
                        }

                        ds.Dispose();
                    }

                    oraComm.Dispose();
                    oraParam = null;

                    return dt;
                }
            }
        }
    }
}
